package hong.jun.lock;

import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * 说明:
 * Created on 2023/10/24 15:44
 *
 * @author yanghongjun <1737619387@qq.com>
 */
public class LockData {
    /**
     * 可重入的读写锁
     */
    private final ReentrantReadWriteLock locker;

    private int count = 0;

    public ReentrantReadWriteLock getLocker() {
        return locker;
    }

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    /**
     * 写加锁
     */
    public void lock() {
        locker.writeLock().lock();
    }

    /**
     * 写解锁
     */
    public void unlock() {
        locker.writeLock().unlock();
    }

    /**
     * 写加锁
     */
    public boolean tryLock() {
        return locker.writeLock().tryLock();
    }

    /**
     * 读加锁
     */
    public void rLock() {
        locker.readLock().lock();
    }

    /**
     * 读解锁
     */
    public void rUnlock() {
        locker.readLock().unlock();
    }

    /**
     * 读加锁
     */
    public boolean rTryLock() {
        return locker.readLock().tryLock();
    }

    public LockData() {
        this.locker = new ReentrantReadWriteLock();
    }
}
